package com.example.shop_2207.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2207.MainActivity
import com.example.shop_2207.R
import com.example.shop_2207.database.MyDbUtils
import com.example.shop_2207.databinding.ActivityLoginBinding
import com.example.shop_2207.mvvm.BaseActivity
import com.example.shop_2207.register.RegisterActivity
@Route(path = "/app/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun getLayout(): Int  = R.layout.activity_login

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java
    @JvmField
    @Autowired
    var username:String = ""
    @JvmField
    @Autowired
    var password:String = ""

    override fun initView() {
        //接收注册成功，返回的值   一定判断不为空时执行   否则报错
       /* val extras = intent.extras
        if(extras!=null){
            val name = extras.get("username").toString()
            val pwd = extras.get("password").toString()
            binding.etUsername.setText(name)
            binding.etPassword.setText(pwd)
        }*/
        ARouter.getInstance().inject(this)
        Log.d("sss", "initView: $username $password")
        binding.etUsername.setText(username)
        binding.etPassword.setText(password)



        binding.btnLogin.setOnClickListener {
            val username = binding.etUsername.text.toString()//获取输入框中的内容
            val password = binding.etPassword.text.toString()
            if(username.isEmpty()||password.isEmpty()){
                ToastUtils.showLong("用户名密码不能为空")
            }else if(!RegexUtils.isMobileSimple(username)){
                ToastUtils.showLong("用户名格式不正确，应该为手机号")
            }else if(!RegexUtils.isMatch("^[\\w]{6,8}\$",password)){
                ToastUtils.showLong("密码格式不正确，应该为6-8位的数字、字母、_组合")
            }else{
                viewModel.login(username,password)
            }
        }
        binding.goRegister.setOnClickListener {
            /*startActivity(Intent(this,RegisterActivity::class.java))*/
            ARouter.getInstance().build("/app/RegisterActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.loginSuccess.observe(this){
            //loginSuccess包括两种结果  一种是code==200 登录成功  另一种code!=200  请求成功了，但未登录成功
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //保存token字段，方便后续请求时，能够正确添加请求头
                SPUtils.getInstance().put("token",it.data.token)
                //保存用户信息
                //保存用户id,方便在我的界面中去进行查询展示---》
                SPUtils.getInstance().put("user_id",it.data.id)
                MyDbUtils.getDb().getUserDao().insertUser(it.data)
                //后续进行处理
                val intent = Intent(this,MainActivity::class.java)
                startActivity(intent)

            }
        }
        viewModel.loginFail.observe(this){
            ToastUtils.showLong(it)
        }
    }



}